﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//using CommonControls.ClassFiles;
using Microsoft.Advertising.Mobile.UI;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.Data;
using OnlineVideos.Common;

using Common;
using Common.Utilities;
using OnlineVideos.Entities;
using Microsoft.Phone.Shell;

namespace OnlineVideos.Views
{
    public partial class ScoreBoard : PhoneApplicationPage
    {
        List<MatchBowlingScore> objbowlinginnings = null;
        List<MatchBattingScore> objbattinginnings = null;
        MatchExtras objextras = null;
       
        private bool IsDataLoaded;

        public ScoreBoard()
        {
            InitializeComponent();
            objbattinginnings = new List<MatchBattingScore>();
            objbowlinginnings = new List<MatchBowlingScore>();
            objextras = new MatchExtras();
            IsDataLoaded = false;
        }
        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(mainGrid, adstaCast, 1);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadAds();
            if (IsDataLoaded == false)
            {
               
                GetPageDataInBackground();
                tblkVideosTitle.Text = CricketMatch.GetMovieTitle(AppSettings.ShowID);
                IsDataLoaded = true;
            }
            performanceProgressBar.IsIndeterminate = false;
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            performanceProgressBar.IsIndeterminate = true;
        }

        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {

                                            a.Result = CricketMatch.LoadBattingInnings(AppSettings.ShowID, TeamType.TeamABatting);
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {

                                            objbattinginnings = (List<MatchBattingScore>)a.Result;
                                            if (objbattinginnings.Count > 0)
                                            {
                                                lbxteamA.ItemsSource = objbattinginnings;
                                                //tblkFavNoMovies.Visibility = Visibility.Collapsed;

                                            }
                                            else
                                            {
                                                //tblkFavNoMovies.Text = "No " + AppResources.ShowDetailPageChaptersPivotTitle + " available";
                                                //tblkFavNoMovies.Visibility = Visibility.Visible;
                                            }

                                        }
                                      );
            bwHelper.AddBackgroundTask(
                                      (object s, DoWorkEventArgs a) =>
                                      {

                                          a.Result = CricketMatch.LoadBattingInnings(AppSettings.ShowID, TeamType.TeamBBatting);
                                      },
                                      (object s, RunWorkerCompletedEventArgs a) =>
                                      {

                                          objbattinginnings = (List<MatchBattingScore>)a.Result;
                                          if (objbattinginnings.Count > 0)
                                          {
                                              lbxteamB.ItemsSource = objbattinginnings;
                                              //tblkFavNoMovies.Visibility = Visibility.Collapsed;

                                          }
                                          else
                                          {
                                              //tblkFavNoMovies.Text = "No " + AppResources.ShowDetailPageChaptersPivotTitle + " available";
                                              //tblkFavNoMovies.Visibility = Visibility.Visible;
                                          }

                                      }
                                    );
            bwHelper.AddBackgroundTask(
                                       (object s, DoWorkEventArgs a) =>
                                       {

                                           a.Result = CricketMatch.LoadbowlingInnings(AppSettings.ShowID, TeamType.TeamABowling);
                                       },
                                       (object s, RunWorkerCompletedEventArgs a) =>
                                       {

                                           objbowlinginnings = (List<MatchBowlingScore>)a.Result;
                                           if (objbowlinginnings.Count > 0)
                                           {
                                               lbxteamAbowling.ItemsSource = objbowlinginnings;
                                               //tblkFavNoMovies.Visibility = Visibility.Collapsed;

                                           }
                                           else
                                           {
                                               //tblkFavNoMovies.Text = "No " + AppResources.ShowDetailPageChaptersPivotTitle + " available";
                                               //tblkFavNoMovies.Visibility = Visibility.Visible;
                                           }

                                       }
                                     );
            bwHelper.AddBackgroundTask(
                                      (object s, DoWorkEventArgs a) =>
                                      {

                                          a.Result = CricketMatch.LoadbowlingInnings(AppSettings.ShowID, TeamType.TeamBBowling);
                                      },
                                      (object s, RunWorkerCompletedEventArgs a) =>
                                      {

                                          objbowlinginnings = (List<MatchBowlingScore>)a.Result;
                                          if (objbowlinginnings.Count > 0)
                                          {
                                              lbxteamBbowling.ItemsSource = objbowlinginnings;
                                              //tblkFavNoMovies.Visibility = Visibility.Collapsed;

                                          }
                                          else
                                          {
                                              //tblkFavNoMovies.Text = "No " + AppResources.ShowDetailPageChaptersPivotTitle + " available";
                                              //tblkFavNoMovies.Visibility = Visibility.Visible;
                                          }

                                      }
                                    );

            bwHelper.AddBackgroundTask(
                                   (object s, DoWorkEventArgs a) =>
                                   {

                                       a.Result = CricketMatch.GetExtraSection(AppSettings.ShowID);
                                   },
                                   (object s, RunWorkerCompletedEventArgs a) =>
                                   {
                                       objextras = (MatchExtras)a.Result;
                                       if (objextras != null)
                                       {
                                           tblkExtras.Text = objextras.TeamAExtras;
                                           tblkTotal.Text = objextras.TeamATotal;
                                           tblkAinnings.Text = objextras.TeamAInnings;
                                           tblkTeamAbatting.Visibility = Visibility.Visible;
                                           txblkExtras.Text = objextras.TeamBExtras;
                                           tblkAExtas.Visibility = Visibility.Visible;
                                           txblkTotal.Text = objextras.TeamBTotal;
                                           tblkATotal.Visibility = Visibility.Visible;
                                           tblkBinnings.Text = objextras.TeamBInnings;
                                       }
                                       else
                                       {
                                           //tblkFavNoMovies.Text = "No " + AppResources.ShowDetailPageChaptersPivotTitle + " available";
                                           //tblkFavNoMovies.Visibility = Visibility.Visible;
                                       }
                                   }
                                 );




            bwHelper.RunBackgroundWorkers();

        }
        private void LoadTitles()
        {


            tblkATeamBat.Text = AppSettings.TeamATitle;
            tblkBTeamBowl.Text = AppSettings.TeamBTitle;
            tblkBTeamBat.Text = AppSettings.TeamBTitle;
            tblkATeamBowl.Text = AppSettings.TeamATitle;
        }


        private void imgTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void pvtScoreBoard_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadTitles();
        }
       

    }
}